"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 视图
Case Name   : 用户没有public权限，创建视图时definer为该用户，使用or replace，合理报错
Create At   : 2023.05.05
Owner       : @li-xin12345
Description :
    1、创建b库
    2、创建用户，用户没有public权限
    3、创建基表
    4、创建视图
    5、创建视图不加replace，definer为步骤2用户
    6、创建视图时，使用or replace，definer为步骤2用户
    7、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
    5、合理报错
    6、合理报错
    7、成功
History     :
"""

import os
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class ViewTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.user_node = Node('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = 'db_view_0001'
        self.created_user = 'u_view_0001'
        self.view_name = 'v_view_0001'
        self.tb_name = 't_view_0001'
        self.error_msg = 'ERROR:  permission denied for schema public'

    def test_view(self):
        text = '-----step1:创建兼容b库数据库;expect:成功-----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} " \
                  f"dbcompatibility ='B' encoding ='UTF8';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd, dbname='postgres')
        self.log.info(sql_res)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, sql_res,
                      '执行失败' + text)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res,
                      '执行失败' + text)

        text = '-----b库下查询dolphin插件-----'
        self.log.info(text)
        sql_cmd = "select extname from pg_extension where extname ='dolphin';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.log.info(sql_res)
        self.assertIn('extname', sql_res, '查询失败')
        self.assertIn('dolphin', sql_res, '执行失败' + text)

        text = '-----step2:创建用户，用户没有public权限; expect:成功-----'
        self.log.info(text)
        sql_cmd = f"drop user if exists {self.created_user};" \
                  f"create user {self.created_user} password" \
                  f" '{macro.COMMON_PASSWD}';"
        self.log.info(sql_cmd)
        res = self.sh_primary.execut_db_sql(sql_cmd)
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG, res)
        self.assertIn(self.constant.CREATE_ROLE_SUCCESS_MSG, res)

        text = '-----step3:创建基表; expect:成功-----'
        self.log.info(text)
        sql_cmd = f'drop table if exists {self.tb_name} cascade;' \
                  f'create table {self.tb_name}(c1 int, c2 varchar(20));'
        self.log.info(sql_cmd)
        res = self.sh_primary.execut_db_sql(sql_cmd, dbname=self.db_name)
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, res, '删表失败')
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS, res, '建表失败')

        text = '-----step4:创建视图; expect:成功-----'
        self.log.info(text)
        sql = f'create view {self.view_name} as select * from {self.tb_name};'
        self.log.info(sql)
        res = self.sh_primary.execut_db_sql(sql, dbname=self.db_name)
        self.assertIn(self.constant.CREATE_VIEW_SUCCESS_MSG, res, '建视图失败')

        text = '-----step5:创建视图不加replace，definer为步骤2用户;' \
               ' expect:合理报错-----'
        self.log.info(text)
        sql = f'create definer={self.created_user} view {self.view_name}' \
              f' as select * from {self.tb_name};'
        self.log.info(sql)
        res = self.sh_primary.execut_db_sql(sql, dbname=self.db_name)
        self.assertIn(self.error_msg, res)

        text = '-----step6:创建视图时，使用or replace，definer为步骤2用户;' \
               ' expect:合理报错-----'
        self.log.info(text)
        sql = f'create or replace definer={self.created_user} ' \
              f'view {self.view_name} as select * from {self.tb_name};'
        self.log.info(sql)
        res = self.sh_primary.execut_db_sql(sql, dbname=self.db_name)
        self.assertIn(self.error_msg, res)

    def tearDown(self):
        self.log.info('-----step7:清理环境; expect:成功-----')
        drop_cmd = f"drop database if exists {self.db_name};" \
                  f"drop table if exists {self.tb_name} cascade;"
        self.log.info(drop_cmd)
        drop_res = self.sh_primary.execut_db_sql(drop_cmd)
        self.log.info(drop_res)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_res, '删库失败')
        self.assertIn(self.constant.DROP_TABLE_SUCCESS, drop_res, '删表失败')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')